<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        // 给定一个排序数组和一个目标值，
        // 在数组中找到目标值，并返回其索引。
        // 如果目标值不存在于数组中，返回它将会被按顺序插入的位置。
        var searchInsert = function (nums, target) {
            let left = 0;
            let right = nums.length - 1;

            if (nums.length == 0) {
                return 0;
            }
            if (nums[right] < target) {
                return nums.length;
            }

            while (left < right) {
                let mid = parseInt(left + (right - left) / 2);
                if (nums[mid] < target) {
                    left = mid + 1;
                } else {
                    right = mid;
                }
            }
            return left;
        };
    </script>
</head>

<body>

</body>

</html>